@page "/"
@using BlazorDatasheet.DataStructures.Geometry
@using BlazorDatasheet.Edit
@using BlazorDatasheet.Render
@using BlazorDatasheet.SharedPages.Data
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.Core.Edit
@using BlazorDatasheet.Core.Formats
@using BlazorDatasheet.Core.Formats.DefaultConditionalFormats
@using BlazorDatasheet.Core.Validation
@using System.Drawing

<PageTitle>Index</PageTitle>

<p>
    Blazor DataSheet is an excel-like editor for cellular values. Below shows some of the features of the data sheet. Sample code is available on <a href="https://github.com/anmcgrath/BlazorDatasheet">GitHub</a>
</p>

<div style="overflow-y: auto; overflow-x: scroll; height: 500px; width: 800px; border: 1px solid #51585e; border-radius: 8px;">
    <Datasheet
        @ref="_datasheet"
        Theme="@Theme"
        Sheet="Sheet"
        ShowColHeadings="true"
        ShowRowHeadings="true"
        StickyHeadings="true">
    </Datasheet>
</div>

<div>
    <button @onclick="InsertRowAfterSelection">Insert row</button>
    <button @onclick="InsertColAfterSelection">Insert col</button>
    <button @onclick="RemoveCol">RemoveCol</button>
    <button @onclick="RemoveRow">RemoveRow</button>
    <button @onclick="() => Sheet.Commands.Undo()">Undo</button>
    <button @onclick="() => Sheet.Commands.Redo()">Redo</button>
    <button @onclick="@(() => Sheet.Selection.Value = "Bob")">Set to Bob</button>
    <button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat() { BackgroundColor = "red" }))">Set bg to red</button>
    <button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat() { BackgroundColor = "blue" }))">Set bg to blue</button>
    <button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat() { ForegroundColor = "green" }))">Set font colour to green</button>
    <button @onclick="@(() => Sheet.Cells.Merge(Sheet.Selection.Regions))">Merge cells</button>
</div>

<div>
    <input id="darkmode-toggle" @onclick="() => _datasheet.ForceReRender()" type="checkbox" @bind="IsDarkMode"/>
    <label for="darkmode-toggle">Darkmode</label>
</div>

@code {

    private Sheet Sheet;
    private string Theme => IsDarkMode ? "dark" : "default";
    private bool IsDarkMode { get; set; }
    private Datasheet _datasheet;

    private void InsertRowAfterSelection()
    {
        if (Sheet.Selection.ActiveRegion == null)
        {
            return;
        }
        Sheet.Rows.InsertRowAt(Sheet.Selection.ActiveRegion.TopLeft.row);
    }

    private void InsertColAfterSelection()
    {
        if (Sheet.Selection.ActiveRegion == null)
        {
            return;
        }
        Sheet.Columns.InsertAt(Sheet.Selection.ActiveRegion.TopLeft.col);
    }

    private void RemoveCol()
    {
        if (Sheet.Selection.ActiveRegion == null)
        {
            return;
        }
        Sheet.Columns.RemoveAt(Sheet.Selection.ActiveRegion.Left);
    }

    private void RemoveRow()
    {
        if (Sheet.Selection.ActiveRegion == null)
        {
            return;
        }
        Sheet.Rows.RemoveAt(Sheet.Selection.ActiveRegion.Top);
    }


    protected override void OnInitialized()
    {
        var src = new SourceValidator(new List<string>()
        {
            "Option 1",
            "Option 2"
        }, false);

        Sheet = new Sheet(100, 100);
        Sheet.BatchUpdates();
        Sheet.Commands.PauseHistory();
        Sheet.Validators.Add(4, 0, new NumberValidator(true));
        Sheet.Validators.Add(1, 3, src);

        Sheet.Cells[0, 0].Value = "Blazor datasheet";

        Sheet.Cells[3, 3].Value = "Date/Time:";
        Sheet.Cells[3, 4].Type = "datetime";
        Sheet.Cells[3, 4].Value = DateTime.Now;

        Sheet.Cells[4, 3].Value = "Select:";
        Sheet.Cells[4, 4].Type = "select";
        Sheet.Cells[4, 4].Value = src.ValidationSource[0];
        Sheet.Validators.Add(4, 4, src);

        Sheet.Cells[5, 3].Value = "Bool:";
        Sheet.Cells[5, 4].Type = "boolean";
        Sheet.Cells[5, 4].Value = false;

        Sheet.Cells.Merge(new Region(14, 15, 4, 5));

        for (int i = 1; i < 5; i++)
        {
            Sheet.Cells[i, 1].Value = i * 2;
        }

        Sheet.Cells[5, 0].Value = "Sum:";
        Sheet.Cells[5, 1].Formula = "=SUM(B2:B5)";

        Sheet.SetFormat(new Region(8, 10, 0, 4), new CellFormat() { BackgroundColor = "#c8d9ed" });
        Sheet.SetFormat(new Region(3, 5, 3, 3), new CellFormat() { TextAlign = "right" });

        for (int i = 14; i <= 17; i++)
        {
            for (int j = 0; j < 4; j++)
                Sheet.Cells[i, j].Value = i + j;
        }

        Sheet.ConditionalFormats.Apply(new Region(14, 17, 0, 4), new NumberScaleConditionalFormat(Color.Aqua, Color.Chocolate));

        Sheet.Rows.SetHeadings(7, 7, "Custom Row");
        Sheet.Columns.SetHeadings(5, 5, "Custom Col");

        Sheet.EndBatchUpdates();
        Sheet.Commands.ResumeHistory();
    }

}